package cn.doways.api.auth;

import cn.doways.api.entity.Secret;
import cn.doways.api.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @ClassName JwtUserDetailsServiceImpl
 * @Description //TODO
 * @Author shellming
 * @Date 2019-08-14 06:08
 **/
@Service
public class JwtUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public UserDetails loadUserByUsername(String phoneNumber) throws UsernameNotFoundException {
        Secret secret= usersMapper.authStudent(phoneNumber);
        if(secret == null){
            throw new UsernameNotFoundException(String.format("No user found with username '%s'.", phoneNumber));
        }else {
            return JwtUserFactory.create(secret);
        }
    }
}
